<!DOCTYPE html>
<html>
<head>
<title>Fig 2.50 - Three Slits</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
  }
  canvas {
    background-color: #ffffff;
    border: 1px solid #ccc;
  }
</style>
</head>
<body>
<canvas id="threeSlitDiagram" width="500" height="600"></canvas>
<script>
  const canvas = document.getElementById('threeSlitDiagram');
  const ctx = canvas.getContext('2d');

  // --- Style and parameters ---
  ctx.strokeStyle = 'black';
  ctx.fillStyle = 'black';
  ctx.lineWidth = 4;
  ctx.lineCap = 'butt'; // 'butt' is closer to the original's sharp ends than 'round'

  const centerX = canvas.width / 2;
  const slitY = 180;
  const slitHeight = 200;
  const slitWidth = 35;
  const d_spacing = 120; // Center-to-center spacing 'd'

  // --- Draw the three slits ---
  // Calculate x-positions based on center alignment
  const leftSlitX = centerX - d_spacing - slitWidth / 2;
  const centerSlitX = centerX - slitWidth / 2;
  const rightSlitX = centerX + d_spacing - slitWidth / 2;

  ctx.strokeRect(leftSlitX, slitY, slitWidth, slitHeight);
  ctx.strokeRect(centerSlitX, slitY, slitWidth, slitHeight);
  ctx.strokeRect(rightSlitX, slitY, slitWidth, slitHeight);

  // --- Draw top dimension lines for 'd' ---
  const dimLineTopY = slitY - 50;
  const tickHeightTop = 20;

  const leftSlitCenterX = centerX - d_spacing;
  const centerSlitCenterX = centerX;
  const rightSlitCenterX = centerX + d_spacing;

  ctx.beginPath();
  // Horizontal line connecting the centers
  ctx.moveTo(leftSlitCenterX, dimLineTopY);
  ctx.lineTo(rightSlitCenterX, dimLineTopY);
  // Ticks at the center of each slit
  ctx.moveTo(leftSlitCenterX, dimLineTopY - tickHeightTop / 2);
  ctx.lineTo(leftSlitCenterX, dimLineTopY + tickHeightTop / 2);
  ctx.moveTo(centerSlitCenterX, dimLineTopY - tickHeightTop / 2);
  ctx.lineTo(centerSlitCenterX, dimLineTopY + tickHeightTop / 2);
  ctx.moveTo(rightSlitCenterX, dimLineTopY - tickHeightTop / 2);
  ctx.lineTo(rightSlitCenterX, dimLineTopY + tickHeightTop / 2);
  ctx.stroke();

  // Labels for 'd'
  ctx.font = 'italic 28px serif';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'bottom';
  ctx.fillText('d', (leftSlitCenterX + centerSlitCenterX) / 2, dimLineTopY - 5);
  ctx.fillText('d', (centerSlitCenterX + rightSlitCenterX) / 2, dimLineTopY - 5);

  // --- Draw bottom dimension lines for 'w' ---
  const dimLineBottomY = slitY + slitHeight + 50;
  const tickHeightBottom = 20;
  const leaderLength = 30;

  const leftSlitEdge1 = leftSlitX;
  const leftSlitEdge2 = leftSlitX + slitWidth;
  const rightSlitEdge1 = rightSlitX;
  const rightSlitEdge2 = rightSlitX + slitWidth;

  // Draw the two separate dimension indicators for 'w'
  ctx.beginPath();
  // Left 'w' dimension
  ctx.moveTo(leftSlitEdge1 - leaderLength, dimLineBottomY); // Outward leader line
  ctx.lineTo(leftSlitEdge2, dimLineBottomY); // Main dimension line
  ctx.moveTo(leftSlitEdge1, dimLineBottomY - tickHeightBottom / 2); // Tick 1
  ctx.lineTo(leftSlitEdge1, dimLineBottomY + tickHeightBottom / 2);
  ctx.moveTo(leftSlitEdge2, dimLineBottomY - tickHeightBottom / 2); // Tick 2
  ctx.lineTo(leftSlitEdge2, dimLineBottomY + tickHeightBottom / 2);

  // Right 'w' dimension
  ctx.moveTo(rightSlitEdge1, dimLineBottomY); // Main dimension line
  ctx.lineTo(rightSlitEdge2 + leaderLength, dimLineBottomY); // Outward leader line
  ctx.moveTo(rightSlitEdge1, dimLineBottomY - tickHeightBottom / 2); // Tick 3
  ctx.lineTo(rightSlitEdge1, dimLineBottomY + tickHeightBottom / 2);
  ctx.moveTo(rightSlitEdge2, dimLineBottomY - tickHeightBottom / 2); // Tick 4
  ctx.lineTo(rightSlitEdge2, dimLineBottomY + tickHeightBottom / 2);
  ctx.stroke();

  // Labels for 'w'
  ctx.font = 'italic 28px serif';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'top';
  ctx.fillText('w', leftSlitEdge1 + slitWidth / 2, dimLineBottomY + 10);
  ctx.fillText('w', rightSlitEdge1 + slitWidth / 2, dimLineBottomY + 10);

  // --- Draw figure caption ---
  const captionY = canvas.height - 50;
  ctx.font = 'bold 32px "Times New Roman", serif';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'bottom';
  ctx.fillText('Fig. 2.50', centerX, captionY);
</script>
</body>
</html>